/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package autoUpdate.albumContent;

import autoUpdate.UpdateManager;
import autoUpdate.WatchEntry;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import picasaphotomanager.JFrames.MainJFrame;
import picasaphotomanager.PicasaSession;
import picasaphotomanager.PicasaSessionManager;

/**
 *
 * @author paskud
 */
public class AlbumContentUpdateThread extends Thread {
    private ArrayList<IAlbumContentUpdateListener> _subscribers = new ArrayList<IAlbumContentUpdateListener>();
    private AlbumEntry _album;
    
    public AlbumContentUpdateThread()
    {

    }
    
    @Override
    public void run() {
        WatchEntry watchedAlbum = UpdateManager.getWatchEntry(_album.getId());
        File albumDirectory = new File(watchedAlbum.getLocalPath());
        
        
        if (!albumDirectory.exists())
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, "Photo directory {0} doesn't exist.", albumDirectory.getAbsolutePath());
            
        if (albumDirectory.isFile())
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, "Photo directory {0} doesn't exist, but there is a file by that name.", albumDirectory.getAbsolutePath());
                
        List<String> _watchedEntries = new ArrayList<String>();
        for (String photoKey : watchedAlbum.getChildren().keySet())
            _watchedEntries.add(photoKey);
        

        PicasaSession session = PicasaSessionManager.getSession(_album.getUsername());
        List<PhotoEntry> photos = null;
        
        try {
            photos = session.PicasaClient.getPhotos(_album, "32u", watchedAlbum.getPhotoResolution());
        } catch (IOException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ServiceException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }        
        
        for (PhotoEntry photo : photos)
        {            
            String photoFilename = UpdateManager.getUrlFileName(photo.getMediaContents().get(0).getUrl());
            File photoFile = new File(albumDirectory.getAbsolutePath(), photoFilename);

            WatchEntry watchedPhoto = watchedAlbum.getChildren().get(photo.getId());
            if (watchedPhoto == null) {
                watchedPhoto = new WatchEntry(photo, photoFile.getPath(), watchedAlbum.getAlbumOwner());
                watchedAlbum.getChildren().put(photo.getId(), watchedPhoto);
            }
            
            UpdateManager.UpdateMedia(watchedPhoto);
            
            if (_watchedEntries.contains(photo.getId()))
                _watchedEntries.remove(photo.getId());
        }
        
        for (String photoId : _watchedEntries) {
            WatchEntry watchedPhoto = watchedAlbum.getChildren().get(photoId);
            File photoFile = new File(watchedPhoto.getLocalPath());
            
            UpdateManager.UpdateMedia(watchedPhoto);
        }
        
        OnAlbumContentUpdate(_album);
    }

    private void OnAlbumContentUpdate(AlbumEntry album) {
        for (IAlbumContentUpdateListener subscriber : _subscribers)
            subscriber.AlbumContentUpdate(new AlbumContentUpdateEvent(this, album));
    }
    
    public void addAlbumUpdateAvailableListiner(IAlbumContentUpdateListener listener)
    {
        _subscribers.add(listener);
    }

    /**
     * @return the _album
     */
    public AlbumEntry getAlbum() {
        return _album;
    }

    /**
     * @param album the _album to set
     */
    public void setAlbum(AlbumEntry album) {
        this._album = album;
    }
    
    






}
